
#include "../../inc/Ui/FindMultipointDirectionForm.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

int FindMultipointDirectionForm::FormId = -1;

FindMultipointDirectionForm::FindMultipointDirectionForm(void)
{
	pOptionMenu = null;
	pListString = null;
}

FindMultipointDirectionForm::~FindMultipointDirectionForm(void)
{
}

bool
FindMultipointDirectionForm::Initialize()
{
	Form::Construct(L"FindMultipointDirectionForm");

	return true;
}

result
FindMultipointDirectionForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	pList = static_cast<List*>(GetControl(L"IDC_LIST"));
	pEditField = static_cast<EditField*>(GetControl(L"IDC_Address"));
	Button *pButton = static_cast<Button*>(GetControl(L"IDC_AddAddress"));
	if (pButton != null){
		pButton->AddActionEventListener(*this);
		pButton->SetActionId(ID_BUTTON);
	}

	pOptionMenu = new OptionMenu();
	pOptionMenu->Construct();
	this->SetOptionkeyActionId(ID_OPTIONMENU);
	pOptionMenu->AddActionEventListener(*this);
	this->AddOptionkeyActionListener(*this);

	Application *pApp = Application::GetInstance();
	AppResource *pAppResource = pApp->GetAppResource();
	String temp = L"";

	pAppResource->GetString(L"IDS_FINDMULTIPOINTDIRECTION_MARKALL", temp);
	pOptionMenu->AddItem(temp, ID_OPTIONMENU_MARKALL);

	pAppResource->GetString(L"IDS_FINDMULTIPOINTDIRECTION_UNMARKALL", temp);
	pOptionMenu->AddItem(temp, ID_OPTIONMENU_UNMARKALL);

	pAppResource->GetString(L"IDS_FINDMULTIPOINTDIRECTION_DELETE", temp);
	pOptionMenu->AddItem(temp, ID_OPTIONMENU_DELETE);

	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_OK);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_CANCEL);

	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);

	return r;
}

void FindMultipointDirectionForm::OnActionPerformed (const Osp::Ui::Control &source, int actionId){
	switch (actionId) {
		case ID_OPTIONMENU:
			pOptionMenu->SetShowState(true);
			pOptionMenu->Show();
			break;

		case ID_SOFTKEY_OK:
			OnSoftkeyOk();
			break;

		case ID_SOFTKEY_CANCEL:
			OnSoftkeyCancel();
			break;

		case ID_OPTIONMENU_DELETE:
			DeleteListItem();
			break;

		case ID_OPTIONMENU_MARKALL:
			pList->SetAllItemsChecked(true);
			pList->RequestRedraw(true);
			break;

		case ID_OPTIONMENU_UNMARKALL:
			pList->SetAllItemsChecked(false);
			pList->RequestRedraw(true);
			break;

		case ID_BUTTON:
			AddAddressToList();
			break;

		default:
			break;
	}
}

void FindMultipointDirectionForm::Reset(){
	pListString = null;
	pList->RemoveAllItems();
}

void FindMultipointDirectionForm::OnSoftkeyCancel(){
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);
	pFormManager->SetCurrentForm(WebForm::GetFormId());
}

void FindMultipointDirectionForm::OnSoftkeyOk(){
	if (pListString->GetCount() < 2){
		MessageBox message;
		Application *pApp = Application::GetInstance();
		String err = L"";
		pApp->GetAppResource()->GetString(L"IDS_FINDMULTIPOINTDIRECTION_ERROR", err);
		message.Construct(L"Find direction", err, MSGBOX_STYLE_OK, 30000);
		int i = 0;
		message.ShowAndWait(i);
	}else{
		FormManager *pFormManager = null;
		FormManager::GetInstance(pFormManager);
		Form *pForm = null;
		pFormManager->GetForm(WebForm::GetFormId(), pForm);
		pFormManager->SetCurrentForm(WebForm::GetFormId());
		pForm->SendUserEvent(WebForm::REQUEST_FINDMULTIPOINT, pListString);

	}
}

void FindMultipointDirectionForm::DeleteListItem(){
	int firstIndex = pList->GetFirstCheckedItemIndex();
	if (firstIndex == -1){
		return;
	}
	do{
		pListString->RemoveItems(firstIndex, 1, true);
		firstIndex = pList->GetNextCheckedItemIndexAfter(firstIndex);
	}while(firstIndex != -1);
	pList->RemoveAllCheckedItems();
	pEditField->RequestRedraw(true);
	pList->RequestRedraw(true);
}

void FindMultipointDirectionForm::AddAddressToList(){
	if (pListString == null){
		pListString = new ArrayList();
		pListString->Construct();
	}

	if (pEditField->GetTextLength() <= 0){
		Application *pApp = Application::GetInstance();
		String err = L"";
		pApp->GetAppResource()->GetString(L"IDS_FINDMULTIPOINT_ADDRESS_EMPTY", err);
		MessageBox message;
		message.Construct(L"Find direction", err, MSGBOX_STYLE_OK, 30000);
		int i = 0;
		message.ShowAndWait(i);
	}else{
		String address = pEditField->GetText();
		pListString->Add(*(new String(address)));
		pList->AddItem(&address, null, null, null);
		pEditField->SetText(String(L""));
		pEditField->RequestRedraw(true);
		pList->RequestRedraw(true);
	}

}

result
FindMultipointDirectionForm::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here
	if (pOptionMenu != null){
		delete pOptionMenu;
	}

	if (pListString != null){
		pListString->RemoveAll(true);
		delete pListString;
	}
	return r;
}


